package com.cn.xact.ty_treasured_book.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cn.xact.ty_treasured_book.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class SettingsActivity extends Activity {

    private ListView mSettingsList;
    private SettingsListViewAdapter mSettingsListViewAdapter;

    private String[] mSettingEntries;
    private String[] mSettingDescriptions;

    private List<Map<String, Object>> listItems;

    private static boolean mIsNightMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_settings);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.main_set);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);
        title1.setText(R.string.main_set);

        mSettingEntries = getResources().getStringArray(R.array.settings_entry);
        mSettingDescriptions = getResources().getStringArray(R.array.settings_entry_des);

        mSettingsList = (ListView)findViewById(R.id.setting_list);
        listItems = getListItems();
        mSettingsListViewAdapter = new SettingsListViewAdapter(this, listItems);
        mSettingsList.setAdapter(mSettingsListViewAdapter);
    }

    private List getListItems() {
        List<Map<String, Object>> innnerListItems = new ArrayList<Map<String, Object>>();
        for(int i = 0; i < mSettingEntries.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("sub", mSettingEntries[i]);
            map.put("des", mSettingDescriptions[i]);
            innnerListItems.add(map);
        }
        return innnerListItems;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.settings, menu);
        return true;
    }

    private class SettingsListViewAdapter extends BaseAdapter {

        private Context context;
        private List<Map<String, Object>> listItems;
        private LayoutInflater listContainer;

        public final class ListItemView{
            public TextView sub;
            public TextView des;
     }

        public SettingsListViewAdapter(Context context, List<Map<String, Object>> listItems) {
            this.context = context;
            listContainer = LayoutInflater.from(context);
            this.listItems = listItems;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listItems.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return listItems.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ListItemView  listItemView = null;
            if (convertView == null){
                listItemView = new ListItemView();
                convertView = listContainer.inflate(R.layout.settings_list_item, null);

                listItemView.sub = (TextView)convertView.findViewById(R.id.settings_list_sub_l1);
                listItemView.des = (TextView)convertView.findViewById(R.id.settings_list_sub_l2);
                convertView.setTag(listItemView);
            }else {
                listItemView = (ListItemView)convertView.getTag();
            }

            String tmp = (String)listItems.get(position).get("des");

            listItemView.sub.setText((String)listItems.get(position).get("sub"));
            listItemView.des.setText((String)listItems.get(position).get("des"));

            if (position == 0) {
                ImageView icon = (ImageView)convertView.findViewById(R.id.settings_list_next);
                icon.setImageResource(R.drawable.switcher_off);
                mIsNightMode = false;
                icon.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        if (mIsNightMode) {
                            ((ImageView)arg0).setImageResource(R.drawable.switcher_off);
                            mIsNightMode = false;
                        } else {
                            ((ImageView)arg0).setImageResource(R.drawable.switcher_on);
                            mIsNightMode = true;
                        }
                    }

                });
            }

            return convertView;
        }

    }

}
